package com.nova.android.tvlauncher.ui.fragment

import android.os.Bundle
import com.nova.android.tvlauncher.AppCache
import com.nova.android.tvlauncher.AppCacheX88
import com.nova.android.tvlauncher.bean.Data
import com.nova.android.tvlauncher.bean.Movy
import com.nova.android.tvlauncher.bean.SubData
import com.nova.android.tvlauncher.bean.Top
import com.nova.android.tvlauncher.constants.Constants
import com.nova.android.tvlauncher.databinding.FragmentCommonBinding
import com.nova.android.tvlauncher.databinding.ItemCommonBannerChildBinding
import com.nova.android.tvlauncher.databinding.ItemCommonBannerParentBinding
import com.nova.android.tvlauncher.databinding.ItemHomeLanChildBinding
import com.nova.android.tvlauncher.databinding.ItemHomeParentBinding
import com_github_lib_base.view.rv.addModels
import com_github_lib_base.view.rv.multiTypeAdapter
import com_github_lib_base.base.BaseVMFragment
import com_github_lib_base.base.BaseViewModel
import com_github_lib_base.ext.getDrawableByName

class CommonFragment : BaseVMFragment<FragmentCommonBinding, BaseViewModel>() {

    private var bannerList: Movy? = null
    private var mainIndex: Int = 0

    override fun initView() {

        mainIndex = requireArguments().getInt(Constants.MAIN_INDEX)

        val homeInfo = AppCacheX88.homeInfo

        homeInfo?.let {
            bannerList = it.movies?.get(mainIndex)
        }

        mBind.rvCommon.multiTypeAdapter<Any> {

            registerType {
                match<ItemCommonBannerParentBinding>(
                    predicate = { it is Movy },
                ) { item, position ->
                    val dtoParent = item as Movy
                    rvContent.multiTypeAdapter<Any> {
                        registerType {
                            match<ItemCommonBannerChildBinding>(
                                predicate = { it is Top },
                            ) { item, position ->
                                val dtoChild = item as Top
                                if (dtoChild.isLocal) {
                                    this.ivContent.setImageDrawable(dtoChild.imageUrl?.getDrawableByName())
                                }
                            }
                        }

                    }
                    dtoParent.top?.let {
                        it.let {
                            rvContent.addModels(it)
                        }
                    }

                }
            }



            registerType {
                match<ItemHomeParentBinding>(
                    predicate = { it is SubData },
                ) { item, position ->
                    val dtoParent = item as SubData
                    rvContent.multiTypeAdapter<Any> {
                        registerType {
                            match<ItemHomeLanChildBinding>(
                                predicate = { it is Data },
                            ) { item, position ->
                                val dtoChild = item as Data
                                if (dtoChild.isLocal) {
                                    this.ivContent.setImageDrawable(dtoChild.imageUrl?.getDrawableByName())
                                }
                            }
                        }

                    }
                    dtoParent.datas?.let {
                        it.let {
                            rvContent.addModels(it)
                        }
                    }

                }
            }
        }
        val list = mutableListOf<Any>()
        bannerList?.let { list.add(it) }
        homeInfo?.movies?.get(mainIndex)?.subData?.let { list.addAll(it) }
        mBind.rvCommon.addModels(list)


    }

    companion object {
        @JvmStatic
        fun newInstance(index: Int): CommonFragment {
            val args = Bundle()
            args.putInt(Constants.MAIN_INDEX, index)
            val fragment = CommonFragment()
            fragment.setArguments(args)
            return fragment
        }
    }

}